package com.hcr.elecident;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.baidu.idl.face.platform.FaceSDKManager;

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;

public class PageActivity extends Activity {

    WebView webView;
    String fileName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page);
       com.hcr.elecident.ActivityManager.getActivityManager().popActivity(this);

        webView = (WebView) findViewById(R.id.mypage);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //允许webview对文件的操作
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowFileAccessFromFileURLs(true);

        // 支持localstore
        webSettings.setDomStorageEnabled(true);

        webSettings.setDefaultTextEncodingName("utf-8");
        webView.setWebChromeClient(new chromClient());


        webView.addJavascriptInterface(this,"nativeMethod");

        String name = getIntent().getStringExtra("name");
        String num = getIntent().getStringExtra("num");
        fileName = getIntent().getStringExtra("fileName");

        if (num == null){
            num = WelcomeActivity.myNum;
        }
        if (name == null){
            name = WelcomeActivity.myName;
        }
        if (fileName == null){
            fileName = WelcomeActivity.myPhoto;
        }

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        webView.loadUrl("file:///android_asset/6.html?name="+name+"&num="+num);
    }

    private class chromClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {

            if (newProgress == 100) {
                //页面加载完成执行的操作
                String path = "file://" + fileName;
                String action = "javascript:aa('" + URLEncoder.encode(path) + "')";
                runWebView(action);
            }
            super.onProgressChanged(view, newProgress);
        }
    }

    private void runWebView(final String url){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webView.loadUrl(url);
            }
        });
    }

    @JavascriptInterface
    public void scan() {
        startActivity(new Intent(this,ScanActivity.class));
    }

    @JavascriptInterface
    public void clear() {
//        com.hcr.elecident.ActivityManager.getActivityManager().popAllActivity();

       int size =  com.hcr.elecident.ActivityManager.getActivityManager().getActivityStackSize();
        System.out.println(size+"!!!!!!!!!!!!JJJJJJJJJJJJJJJJJJJJ");
        SharedPreferences preferences = getSharedPreferences(WelcomeActivity.TEMP_DATA,MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private boolean mIsExit;
//
//    /**
//     * 双击返回键退出
//     */
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//
//            // 返回桌面
//            Intent home = new Intent(Intent.ACTION_MAIN);
//            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            home.addCategory(Intent.CATEGORY_HOME);
//            startActivity(home);
//
////            if (mIsExit) {
////                this.finish();
////
////            } else {
////                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
////                mIsExit = true;
////                new Handler().postDelayed(new Runnable() {
////                    @Override
////                    public void run() {
////                        mIsExit = false;
////
////                    }
////                }, 2000);
////            }
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    @JavascriptInterface
    public void camera(String name,String num,String phone) {

//        requestPermissions(99, Manifest.permission.CAMERA);
        initLib();


        Intent intent = new Intent(this,FaceDetectExpActivity.class);
        intent.putExtra("name",name);
        intent.putExtra("num",num);
        intent.putExtra("phone",phone);

        startActivity(intent);
        finish();

//        startItemActivity(FaceDetectExpActivity.class);
    }

    public void initLib() {
        FaceSDKManager.getInstance().initialize(this, Config.licenseID, Config.licenseFileName);
    }
}
